import api from '../../api';
import { message } from 'antd';
const index = (e) => {
  switch (e) {
    case '0':
      return 'Brinson';
    case '1':
      return 'Campisi';
    case '2':
      return 'Barra';
  }
};

export default {
  namespace: 'riskAttributionM',

  state: {
    MAAT_PRODUCT_INVE_TYPE: [], //投资类型字典
    modelData: [], // 字典值
  },
  effects: {
    //获取字典
    *getModelData({ payload }, { call, put }) {
      const res = payload;
      if (res) {
        const setModel = res.ATTRIBUTION_MODEL;
        let newData = [];
        if (setModel && setModel.length) {
          setModel.map((item) => {
            newData.push({
              title: item.value,
              value: item.code,
            });
          });
        }
        yield put({
          type: 'save',
          payload: {
            modelData: newData,
            MAAT_PRODUCT_INVE_TYPE: res.MAAT_PRODUCT_INVE_TYPE,
          },
        });
      }
    },
    //获取产品指定时间范围期初期末时间
    *queryProductTime({ payload, callback }, { call, put }) {
      const res = yield call(api.riskAttribution.queryProductTime, payload);
      if (res && res.data && res.code == 0) {
        callback(res);
      } else {
        callback(false);
        message.error(res?.msg || '查询时间范围失败！');
      }
    },

    // 获取风险分解
    *getRisk({ payload, callback }, { call, put }) {
      const test = index(payload.setupModel); // 根据不同模型调不同接口
      const res = yield call(api.riskAttribution.getRisk[test], payload);
      if (res && res.code == 0) {
        callback(res.data || []);
      } else {
        callback();
        message.error(res?.msg || '获取风险分解失败！');
      }
    },

    // 获取超额风险趋势图数据
    *getExcessTrend({ payload, callback }, { call, put }) {
      const test = index(payload.setupModel); // 根据不同模型调不同接口
      const res = yield call(api.riskAttribution.getExcessTrend[test], payload);
      if (res && res.code == 0) {
        callback(res.data);
      } else {
        callback();
        message.error(res?.msg || '获取超额风险趋势失败！ ');
      }
    },

    // 获取因子风险趋势图
    *getFactorRiskTrend({ payload, callback }, { call, put }) {
      const res = yield call(api.riskAttribution.getFactorRiskTrend, payload);
      if (res && res.code == 0) {
        callback(res.data);
      } else {
        callback();
        message.error(res?.msg || '获取因子风险失败！');
      }
    },

    // 因子风险贡献
    *getFactorContributionDetail({ payload, callback }, { call, put }) {
      const res = yield call(api.riskAttribution.getFactorContributionDetail, payload);

      if (res && res.code == 0) {
        callback(res);
      } else {
        callback();
        message.error(res?.msg || '查询因子风险贡献失败！');
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
